<?php
/**
 * 订单管理
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author	   NcPHP Team
 * @since      File available since Release v2.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class orderControl extends SystemControl{
	private $links = array(
		array('url'=>'act=order&op=index','lang'=>'nc_manage')
	);

	public function __construct(){
		parent::__construct();
		Language::read('order');
	}

	/**
	 * 订单列表
	 */
	public function indexOp(){
	    /* @var orderModel $model */
        $model = Model('order');
        $table = 'order,member,member_merchant';
        $on = 'member.member_id=order.member_id,member_merchant.merchant_id=order.merchant_id';
        $field = array(
            'order.order_id',
            'order.order_sn',
            'order.ticket_title',
            'order.need_pay',
            'order.add_time',
            'order.order_status',
            'order.merchant_id',
            'order.order_type',
            'order.spec_name',
            'order.if_examine',
            'order.pay_type',
            'member.nickname',
            'member.mobile',
            'member_merchant.mc_shop_name'
        );
        //查询条件
        $condition = array();
	    if (isset($_GET['search_sn']) && $_GET['search_sn']) {
	        $condition['order_sn'] = $_GET['search_sn'];
        }
        if (isset($_GET['search_phone']) && $_GET['search_phone']) {
            $condition['member_phone'] = $_GET['search_phone'];
        }
        $time_from = strtotime($_GET['search_stime']);
        $time_to = strtotime($_GET['search_etime']);

        $day = getgpc('day');
        if($day=='today'){
            $time_from = $time_to = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d"),date("Y")))) ;
        }
        if ($time_from && $time_to) {
            $time_to = $time_to + 86400;
            $condition['add_time'] = array('between',array($time_from,$time_to));
        } elseif ($time_from) {
            $condition['add_time'] = array('egt',$time_from);
        } elseif ($time_to) {
            $time_to = $time_to + 86400;
            $condition['add_time'] = array('elt',$time_to);
        }

        if (isset($_GET['search_status']) && is_numeric($_GET['search_status'])) {
	        $condition['order_status'] = intval($_GET['search_status']);
        }
        if(isset($_GET['order_type']) && is_numeric($_GET['order_type'])){
            $condition['order_type'] = intval($_GET['order_type']);
        }
        $merchant_id = intval($this->admin_info['merchant_id']);
        if($merchant_id>0){
            $condition['order.merchant_id'] = $merchant_id;
        }else{
            if (isset($_GET['search_merchant']) && is_numeric($_GET['search_merchant'])) {
                $condition['order.merchant_id'] = intval($_GET['search_merchant']);
            }
        }
        //订单列表
        $orderList = $model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->order('add_time desc')
            ->page(20)
            ->select();
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        foreach ($orderList as $k => $order) {
            switch($order['order_type']){
                case 1:
                    $orderList[$k]['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                    break;
                case 2:
                    $orderList[$k]['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                    break;
                case 3:
                    $orderList[$k]['sku'] = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
                case 4:
                    $orderList[$k]['sku'] = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
            }
        }

        /* @var member_merchantModel $merchant_model */
        $merchant_model = Model('member_merchant');
        if(intval($this->admin_info['merchant_id'])>0){
            $merchants = $merchant_model->getMerchantList(array('mc_status'=>1,'merchant_id'=>$this->admin_info['merchant_id']), 'merchant_id,mc_shop_name');
        }else{
            $merchants = $merchant_model->getMerchantList(array('mc_status'=>1), 'merchant_id,mc_shop_name');
        }
        Tpl::output('merchants', $merchants);
        Tpl::output('status', $model->status);
        Tpl::output('order_type',$model->type);
        Tpl::output('search', $_GET);
        Tpl::output('orders', $orderList);
        Tpl::output('pay_type',$model->pay_type);
        Tpl::output('page', $model->showpage());
        Tpl::output('top_link',$this->sublink($this->links,'index'));
        Tpl::showpage('order.index');
	}

    /**
     * 确认收款
     */
	public function check_moneyOp()
    {
        /* @var orderModel $model */
        $model = Model('order');

        $order_id = intval($_POST['id']);
        $order = $model->getOrderInfo(array('order_id'=>$order_id, 'order_status'=>10));
        if ($order) {
            $rand_code = '';
            for ($i = 0; $i < 16; $i++) {
                $rand_code .= mt_rand(0, 9);
            }
            $params = array(
                'order_status' => 20,
                'true_pay' => $order['need_pay'],
                'pay_time' => time(),
                'pay_type'=>3,
                'ticket_code' => str_pad($order['member_id']+$order['merchant_id'],16,$rand_code)
            );
            $res = $model->updateOrder(array('order_id'=>$order_id, 'order_status'=>10), $params);
            if ($res) {
                exit(json_encode(array('res'=>1)));
            }
        }
        exit(json_encode(array('res'=>1)));
    }

    /**
     * 订单详情
     */
	public function detailOp(){
	    $order_id = intval($_GET['order_id']);

        /* @var orderModel $model */
        $model = Model('order');
        $table = 'order,member,member_merchant';
        $on = 'member.member_id=order.member_id,member_merchant.merchant_id=order.merchant_id';
        $field = array(
            'order.order_id',
            'order.order_sn',
            'order.ticket_title',
            'order.need_pay',
            'order.pay_time',
            'order.allow_date',
            'order.add_time',
            'order.used_time',
            'order.order_status',
            'order.member_name',
            'order.member_phone',
            'order.member_idcard',
            'order.order_type',
            'order.spec_name',
            'order.ticket_code',
            'member.nickname',
            'member_merchant.mc_shop_name'
        );
        $condition = array('order_id'=>$order_id);
        //订单列表
        $order = $model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->find();

        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        switch($order['order_type']){
            case 1:

                $order['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                break;
            case 2:
                $order['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                break;
            default:
                $order['sku'] = Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                break;


        }

        Tpl::output('order_type',$model->type);
        Tpl::output('status', $model->status);
        Tpl::output('order', $order);
        Tpl::showpage('order.detail');
    }

    /**
     * 导出订单
     */
    public function exportOp(){
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        /* @var order_hotelModel  $order_hotel_model */
        $order_hotel_model = Model('order_hotel');
        /* @var order_goodsModel $order_goods_model */
        $order_goods_model = Model('order_goods');
        /* @var orderModel $model */
        $model = Model('order');
        $table = 'order,member,member_merchant';
        $on = 'member.member_id=order.member_id,member_merchant.merchant_id=order.merchant_id';
        $field = array(
            'order.order_id',
            'order.order_sn',
            'order.ticket_title',
            'order.need_pay',
            'order.add_time',
            'order.order_status',
            'order.order_type',
            'order.spec_name',
            'order.pay_type',
            'member.nickname',
            'member_merchant.mc_shop_name'
        );
        $condition = array();

        //查询条件
        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['order_sn'] = $_GET['search_sn'];
        }
        $time_from = $_GET['search_stime'];
        if(!empty($time_from)){
            $condition['add_time'] = array('egt',strtotime($time_from));
        }
        $time_to = $_GET['search_etime'];
        if(!empty($time_to)){
            $condition['add_time'] = array('elt',strtotime($time_to));
        }
        $merchant_id = intval($this->admin_info['merchant_id']);
        if($merchant_id>0){
            $condition['order.merchant_id'] = $merchant_id;
        }else{
            if (isset($_GET['search_merchant']) && is_numeric($_GET['search_merchant'])) {
                $condition['order.merchant_id'] = intval($_GET['search_merchant']);
            }
        }
        if (isset($_GET['search_status']) && is_numeric($_GET['search_status'])) {
            $condition['order_status'] = intval($_GET['search_status']);
        }

        //每个EXCEL最大条数
        $exportSize = 1000;
        if (!is_numeric($_GET['curpage'])) {
            $count = $model->getOrderCount($condition);
            $array = array();
            if ($count > $exportSize) {
                $page = ceil($count / $exportSize);
                for ($i=1; $i<=$page; $i++){
                    $limit1 = ($i -1 ) * $exportSize + 1;
                    $limit2 = $i * $exportSize > $count ? $count : $i * $exportSize;
                    $array[$i] = $limit1 . ' ~ ' . $limit2 ;
                }
                Tpl::output('list',$array);
                Tpl::showpage('export.excel');
            } else {
                $data = $model
                    ->table($table)->field(implode(',',$field))
                    ->join('left,left')->on($on)
                    ->where($condition)
                    ->order('order_id desc')
                    ->limit($exportSize)
                    ->select();
                foreach ($data as $k=>$order) {
                    switch($order['order_type']){
                        case HOTEL_MARK:
                            //酒店
                            $skus = $order_hotel_model->getOrderHotelList(array('order_id'=>$order['order_id']));
                            break;
                        case TICKET_MARK :
                            //门票
                            $skus = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                            break;
                        case FOOD_MARK :
                            //美食
                            $skus = $order_goods_model->getOrderGoodsList(array('order_id'=>$order['order_id']));
                            break;
                        case ITINERARY_MARK :
                            //旅行社
                            $skus = $order_goods_model->getOrderGoodsList(array('order_id'=>$order['order_id']));
                            break;

                    }
                    foreach ($skus as $i=>$sku) {

                        if($order['order_type'] == HOTEL_MARK){
                            $skus[$i] = $order['spec_name'].'x'.$sku['oh_num'].'合计：'.$sku['numdate'].'晚';
                        }elseif($order['order_type']==TICKET_MARK){
                            $skus[$i] = $sku['type_name'].'x'.$sku['ot_num'];
                        }else{
                            $skus[$i] = $order['spec_name'].'x'.$sku['og_num'];
                        }
                    }
                    $data[$k]['sku'] = implode('/', $skus);
                }
                $this->createExcel($data, $model->status,$model->pay_type);
            }
        } else {
            $limit1 = ($_GET['curpage'] - 1) * $exportSize;
            $limit2 = $exportSize;
            $data = $model
                ->table($table)->field(implode(',',$field))
                ->join('left,left')->on($on)
                ->where($condition)
                ->order('order_id desc')
                ->limit("$limit1,$limit2")
                ->select();
            $this->createExcel($data, $model->status,$model->pay_type);
        }
    }

    /**
     * 创建Excel
     * @param array $data
     * @param array $status
     * @param array $pay_type
     */
    private function createExcel($data, $status ,$pay_type) {
        import('libraries.excel');
        $excel_obj = new Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
        //表头
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_order_sn'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_title'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_nickname'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_content'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_merchant'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_money'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_addtime'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'支付方式');
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_status'));
        //数据
        foreach ((array)$data as $k=>$v){
            $tmp = array();
            $tmp[] = array('data'=>$v['order_sn']);
            $tmp[] = array('data'=>$v['ticket_title']);
            $tmp[] = array('data'=>$v['nickname']);
            $tmp[] = array('data'=>$v['sku']);
            $tmp[] = array('data'=>$v['mc_shop_name']);
            $tmp[] = array('format'=>'Number','data'=>ncPriceFormat($v['need_pay']));
            $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['add_time']));
            $tmp[] = array('data'=>$pay_type[$v['pay_type']]);
            $tmp[] = array('data'=>$status[$v['order_status']]);

            $excel_data[] = $tmp;
        }

        $excel_data = $excel_obj->charset($excel_data,CHARSET);
        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset(L('order_index'),CHARSET));
        $excel_obj->generateXML($excel_obj->charset(L('order_index'),CHARSET).$_GET['curpage'].'-'.date('Y-m-d-H',time()));
    }

    /**
     * 审核订单(针对酒店)
     */
    public function examine_orderOp()
    {
        $order_id = intval($_POST['order_id']);
        $if_examine = intval($_POST['if_examine']);
        require_once BASE_DATA_PATH.'/vendors/clSms/ChuanglanSmsHelper/ChuanglanSmsApi.php';
        $clapi  = new ChuanglanSmsApi();
        if($order_id>0){
            /* @var orderModel $model */
            $model = Model('order');
            $order = $model->getOrderInfo(array('order_id'=>$order_id));
            $order['sku'] = Model('order_hotel')->getOrderHotelInfo(array('order_id'=>$order['order_id']));
            if($order['if_examine']==0 && $order['order_status']==20 && $order['order_type']==1){
                $member_model = Model('member');
                $member_mobile = $member_model->getMemberInfo(array('member_id'=>$order['member_id']),'mobile');
                if($if_examine==1){
                    //确认入驻直接改变订单状态
                    if($model->updateOrder(array('order_id'=>$order_id),array('if_examine'=>1))){
                        //发送信息给客户
                        $msg = '您预订的'.$order['ticket_title'].'订单号'.$order['order_sn'].'已预订成功，登记人'.'【'.$order['member_name'].'】'.'，房型：'.$order['spec_name'].'，房间数：'.$order['sku']['oh_num'].'间，入驻时间：'.$order['sku']['startdate'].'，入驻天数：'.$order['sku']['numdate'].'天。'.'核销码为：'.$order['ticket_code'].',请凭该核销码及时入驻，感谢您使用玩转六盘水APP！';
                        $clapi->sendSMS($member_mobile['mobile'],$msg);
                        $this->log('确认酒店订单入驻'.'[订单号:'.$order['order_sn'].']',1);
                        exit(json_encode(array('res'=>1)));
                    }else{
                        exit(json_encode(array('res'=>0,'_msg'=>'操作失败')));
                    }

                }else{
                    //拒绝入驻改变订单状态并且退款给客户的账户中
                    //生成退款订单
                    /* @var order_refundModel $refund_model */
                    $refund_model = Model('order_refund');
                    $refund_arr = array();
                    $refund_sn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
                    $refund_arr['order_id'] = $order['order_id'];
                    $refund_arr['refund_money'] = $order['true_pay'];
                    $refund_arr['note'] = '酒店房间数量不足';
                    $refund_arr['pay_type'] = $order['pay_type'];
                    $refund_arr['addtimes'] = time();
                    $refund_arr['merchant_id'] = $order['merchant_id'];
                    $refund_arr['member_id'] = $order['member_id'];
                    $refund_arr['member_phone'] = $_SESSION['member']['mobile'];
                    $refund_arr['order_sn'] = $order['order_sn'];
                    $refund_arr['refund_type'] = '商家确认退款,房间数量不足';
                    $refund_arr['pay_sn'] = $order['pay_sn'];
                    $refund_arr['out_refund_no'] = $refund_sn;
                    //$refund_arr['status'] = 1;
                    //$refund_model->addOrderRefund($refund_arr);
                    if($order['pay_type']==1){
                        //微信退款
                        $result = $this->wxRefund($order,$refund_sn);
                        if(($result['return_code']=='SUCCESS') && ($result['result_code']=='SUCCESS')){
                            $refund_arr['status'] = 1;
                            $refund_arr['if_pay'] = 1;
                            $res_1 = $model->updateOrder(array('order_id'=>$order_id),array('if_refund'=>1,'order_status'=>32,'if_examine'=>2)) ;
                            $res = $refund_model->addOrderRefund($refund_arr);
                            if($res && $res_1){
                                $msg = '您预订的'.$order['ticket_title'].'，房型:'.$order['spec_name'].',订单号'.$order['order_sn'].'暂无房源！请选择其他房型！您所支付的金额将原路返回至您的支付账户中，请注意查收！';
                                $clapi->sendSMS($member_mobile['mobile'],$msg);
                                $this->log('拒绝酒店订单'.'[订单号:'.$order['order_sn'].']',1);
                                exit(json_encode(array('res'=>1)));
                            }else{
                                exit(json_encode(array('res'=>0,'_msg'=>'更新数据库失败')));
                            }
                        }elseif(($result['return_code']=='FAIL') || ($result['result_code']=='FAIL')){
                            //退款失败
                            //原因
                            $reason = (empty($result['err_code_des'])?$result['return_msg']:$result['err_code_des']);
                            exit(json_encode(array('res'=>1,'_msg'=>$reason)));
                        }else{
                            exit(json_encode(array('res'=>1,'_msg'=>'订单拒绝失败!请重试')));
                        }
                    }elseif($order['pay_type']==2){
                        //支付宝退款
                        $result = $this->alipayRefund($refund_sn,$order['pay_sn'],$order['need_pay']);
                        if($result['code']==10000 && $result['msg'] =='Success'){
                            //到账成功
                            $refund_arr['status'] = 1;
                            $refund_arr['if_pay'] = 1;
                            $res_1 = $model->updateOrder(array('order_id'=>$order_id),array('if_refund'=>1,'order_status'=>32,'if_examine'=>2));
                            $res = $refund_model->addOrderRefund($refund_arr);
                            if($res_1 && $res){
                                $this->log('拒绝酒店订单'.'[订单号:'.$order['order_sn'].']',1);
                                $msg = '您预订的'.$order['ticket_title'].'，房型:'.$order['spec_name'].',订单号'.$order['order_sn'].'暂无房源！请选择其他房型！您所支付的金额将原路返回至您的支付账户中，请注意查收！';
                                $clapi->sendSMS($member_mobile['mobile'],$msg);
                                exit(json_encode(array('res'=>1)));
                            }else{
                                exit(json_encode(array('res'=>0,'_msg'=>'更新数据库失败')));
                            }
                        }else{
                            exit(json_encode(array('res'=>0,'_msg'=>$result['sub_msg'])));
                        }
                    }else{
                        exit(json_encode(array('res'=>0,'_msg'=>'该订单为后台确认收款,无法更改状态!')));
                    }
                }
            }else{
                exit(json_encode(array('res'=>0,'_msg'=>'非法操作')));
            }
        }

    }

    /**
     * ajax 获取订单信息
     */
    public function ajax_get_orderOp()
    {
        $order_id = intval($_POST['id']);
        if($order_id>0){
            /* @var orderModel $model */
            $model = Model('order');
            $order = $model->getOrderInfo(array('order_id'=>$order_id));
            if($order && is_array($order)){
                /* @var order_hotelModel $order_hotel_model */
                $order_hotel_model = Model('order_hotel');
                switch($order['order_type']){
                    case 1:
                        $order['sku'] = $order_hotel_model->getOrderHotelInfo(array('order_id'=>$order['order_id']));
                        break;
                }
                exit(json_encode($order));
            }
        }
    }

    /**
     * 微信退款
     * @param  array $order_info 订单信息
     * @param string $refund_sn 退款单号
     * @return 成功时返回(array类型)，其他抛异常
     */
    public function wxRefund($order_info,$refund_sn){
        require_once BASE_DATA_PATH."/vendors/weixin/lib/WxPay.Api.php";


        $merchid = WxPayConfig::MCHID;

        $input = new WxPayRefund();
        $input->SetOut_trade_no($order_info['order_sn']);         //自己的订单号
        $input->SetTransaction_id($order_info['pay_sn']);     //微信官方生成的订单流水号，在支付成功中有返回
        $input->SetOut_refund_no($refund_sn);         //退款单号
        $input->SetTotal_fee(intval($order_info['need_pay']*100));         //订单标价金额，单位为分
        $input->SetRefund_fee(intval($order_info['need_pay']*100));            //退款总金额，订单总金额，单位为分，只能为整数
        $input->SetOp_user_id($merchid);
        $result = WxPayApi::refund($input); //退款操作

        // 这句file_put_contents是用来查看服务器返回的退款结果 测试完可以删除了
        //file_put_contents(APP_ROOT.'/Api/wxpay/logs/log3.txt',arrayToXml($result),FILE_APPEND);
        return $result;
    }
    /**
     * 支付宝退货处理
     * 退货前提是必须支付成功
     * @param string $out_trade_no 订单号
     * @param string $wepay_serial
     * @param $totalFee 金额
     */
    public function alipayRefund($out_trade_no,$wepay_serial,$totalFee) {

        require_once BASE_DATA_PATH.'/vendors/alipay/AopClient.php';
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018011101773187';
        $aop->rsaPrivateKey = 'MIIEowIBAAKCAQEAm1P3EZY8AjvdMs0ltJSObEJQIX6EJDxKydOiOeJAodnHqhHtkWAUNeL7YW0klo7ZVJnEdEdxjzjOWpjQHJNgxYLpOCAwTyqOkzmWjWJpLebl2cOQc9mi9DLSMSDb0A4berU+HG7sAcPDUKXWriTbFlnfKtY7pJv0fmyFQiNkJmVEm3Jfuwk9S43MOtHG/e4upw1I2vpZZTg+xEN2NW5vMtug+jHMim4h34/qQno9a+xF0rKJFpYpBa/iF0Au/vyTBgkaD3M1W8niguUlje0QHCbQGvSghoH6htMXTN88aY8Mo2BRGNrFw64r6C+w7kv/pKARIkd3JChJNSolG/tNQwIDAQABAoIBAA326ouGOjbpMuXSU8V8ZaGOBK4+NZscYIzsnfUeRjlG3xCHNBiRzr9ZfMgxi8yNYzSb7nGVyrvZm5PBkVRAW77mGhmVq0IyzUanMsXbRyF9VMTTNDV2J5RUzVjlX13BJu9NRBxCTK2L8A9cGYTmg1vrEIlFYW9XJhAHqaNckzBwXnNKb0NmFugajTZT80JkeROjDYdBWO/BIcBCWUKnEhEtMtsF0G78ZEwsA0cAk5Hqji3BLIqahSPKsLUQm60iKLcMtUIfJV4vRyqDcV54hPSX/gFSDwJhLk/Nnz4kyVDscmTByMaSZI65Sx8AlzTkT8LQ5fqpHWApmRDJOwSvJgECgYEAyRv7NUFEDHJiPx5zpwgz5K7X92V2mEfV0lVtSdmmwlbnWm+gCcWgyx/tJKb6TpngFY0fTMRPD1NpJvyoWR7tTPGsztFG6myw+yZpY0AiUbTgEygvWTlYVL6FnEq/UT32fY1eBzW0TOYlpdW00YpA03jn+aA/nkF9gVUUc55KzwMCgYEAxbkggUsiayXslIu4QWaIQlFcBhsELjXMuH6lytDnRk82UPWZFXXdAvR0vXtaoQadeh8FUbU1NeytxQVVPWGhh1+5yECWrWr8Kzy9SOluIs79oZsAWr9WdOc9tORni668CvMRhBfYplnj3BZ9YjGp/icr+oDafDJ7zYHJwOAjFMECgYEAvtwsY5VGNJ3Vudt3nRz+ythYpDUKAWmjdtOWPdEvHx8Ev+JSOxPxDvWExRUnVrNYZHujKeLguUXK9LIyPypHwY9XFoysxcxThnck64mxSGPliKjJjt8KgpqyCOszeLHanbstcqA4Ngbx0dnomjigX1JOray008eJidWp7DRdAOsCgYAE+l45Ix0guXm1W7ct3lj74nSAqtl2GGCpHHcsHgpUmNXzP9u00JzZ4DG4O4zSlQoN8w39OqS2UNknJrU4sGaSs/RHeGjSwOjMZeEWHtppAfEcynw9zKdulgvVrBJ3T9ToKap9OuR3e7fi6KYLfNicz3up+OiHS/YV/HiJ5Jq9gQKBgH170er5Fg83GlbVK432cU1egBKwPshtrf9bR1miRFSqgj9AThf2B1e5WqvXOXqrHEONv2HeaFDY/0Vt5LT6BaGg1zFu5o0jYalSQoUw9HM+Gb6Einf5+yhV9xoHA6uR2IVbw8vo0SGKuXq9W6ppNNc5xH03abw/KOOx93l7qRLC';
        $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAitkV3HWbV9DUqDRPncMnwc0J4zFehoaWzeMtD2ktpMXO/EqcETgmL62hAdKzfFBDuRQ4pOovlFCXPV5PBRwLdcJL3XXdDnem1cPt4dfbAlpfhi+Q31Fkkv97rV56SRIkbBVIRx4PMVr8+Xbk0KPzF+TlxJrCFgxok2fwO7d2izdefH2BR0UMJ7q8r3cllMe7LwTy6tFwa0LnUap0liBRLTBGuU5lNIQTtnjxjjOkfHJ9GpjnHsmbecFADsFPj2sruDr+lT86QBqVfiAs6KBiTYgV+zyW95uBSWD14+jhG7OAZQvEiPsri4ZXiAViSDBRg/KR2DAdOvCu8Ek0mINFLQIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipayTradeRefundRequest();
        $bizcontent = json_encode([
            'out_trade_no'=>$out_trade_no,
            'trade_no'=> $wepay_serial,
            'refund_amount'=> $totalFee,
            'refund_reason'=>'正常退款'
        ]);
        $request->setBizContent($bizcontent);
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        //$resultCode = $result->$responseNode->code;
        $result = object_array($result->$responseNode);
        return $result;
        /*echo $resultCode;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "退款成功";
        } else {
            echo "退款失败";
        }*/
    }


}
